//
//  DMPathemaDetailController.m
//  MCNKuaiYi
//
//  Created by ling on 17/1/8.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMPathemaDetailController.h"
#import "PathemaDetailCell.h"
#import "DMpathemaDetailModel.h"

static NSString *rid = @"cell";

@interface DMPathemaDetailController ()<UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSArray<DMpathemaDetailModel *> *pathemaDetailList;

@property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader;

@end

@implementation DMPathemaDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    
    [self loadData];
}


#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _pathemaDetailList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PathemaDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:rid forIndexPath:indexPath];
    DMpathemaDetailModel *model = _pathemaDetailList[indexPath.row];
    
    cell.model = model;
    
    return cell;
}


#pragma mark - 加载数据
- (void)loadData
{
    
    //1. 创建url
    //返回当前的时间戳
    BOOL isPullUp = YES;
    
    //如果下拉刷新正在招待
    isPullUp = !_refreshHeader.isRefreshing;
    
    NSInteger currentTime = [[NSDate date] timeIntervalSince1970];
    if (currentTime == 0) {
        currentTime = [[NSDate date] timeIntervalSince1970];
    }
 
    NSString *strURL = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    
    //疾病详细的id
    NSInteger id = _id;
    
    NSDictionary *parameters = @{@"page":@1,@"page_size":@15,@"ci1_id":@(id),@"keyword":@""};
    
    [DMpathemaDetailModel getPathemaDetailDataWithURLString:strURL andParameters:parameters andSuccessBlock:^(NSArray *newsList) {
       
        _pathemaDetailList = newsList;

        dispatch_async(dispatch_get_main_queue(), ^{
            
            [_tableView reloadData];
            
            //如果是上拉
            if (isPullUp == NO) [_refreshHeader endRefreshing];
            
        });
        
    }];
    
    
    
    
}

#pragma mark - 搭建界面
- (void)setupUI
{
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    _tableView.rowHeight = 60;
    
    _tableView.dataSource = self;
    //注册cell
    [_tableView registerClass:[PathemaDetailCell class] forCellReuseIdentifier:rid];
    
    [self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
